package com.bwie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.bwie.common.constants.Constants;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Role;
import com.bwie.common.domain.User;
import com.bwie.common.domain.response.UserLoginRes;
import com.bwie.common.exceptions.BizExceptions;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.remote.UserRemote;
import com.bwie.service.UserServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @className: UserServcieImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 汪菲阳
 * @date: 2025/8/24 11:26
 */
@Service
public class UserServcieImpl implements UserServcie {
    @Autowired
    private UserRemote userRemote;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    @Override
    public UserLoginRes login(User user) {
        //远程调用
        Result<User> login = userRemote.login(user);
        //熔断
        if (login.getCode()== Constants.ERROR){
            throw new BizExceptions(1001,"服务器繁忙，请稍候再试……");
        }
        //获取查询到的数据
        User loginData = login.getData();
        //非空
        if (loginData==null){
            throw new BizExceptions(1002,"该用户未注册");
        }
        //密码必须包含字母和数字
        if (!user.getUserPassword().matches("^(?=.*[A-Za-z])(?=.*\\d).+$")){
            throw new BizExceptions(1006,"密码必须包含字母和数字");
        }
        //判断密码是否正确
        if (!user.getUserPassword().equals(loginData.getUserPassword())){
            throw new BizExceptions(1003,"密码输入错误");
        }
        //生成token
        HashMap<String, Object> map = new HashMap<>();
        String userKey = IdUtil.fastSimpleUUID();
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        //存入redis
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey,loginData,30, TimeUnit.MINUTES);
        //角色列表
        Result<List<Role>> roleList = userRemote.getRoleList(loginData.getUserId());
        //熔断
        if (roleList.getCode()==Constants.ERROR){
            throw new BizExceptions(1004,"服务器繁忙，请稍候再试……");
        }
        List<Role> roleListData = roleList.getData();
        //非空
        if (roleListData==null){
            throw new BizExceptions(1005,"该用户没有角色");
        }
        UserLoginRes userLoginRes = new UserLoginRes();
        userLoginRes.setToken(token);
        userLoginRes.setRoleList(roleListData);
        return userLoginRes;
    }

    @Override
    public String sureRole(Integer roleId) {
        //获取token
        String token = request.getHeader(TokenConstants.TOKEN);
        //获取userKey
        String userKey = JwtUtils.getUserKey(token);
        //保存选择的角色id
        redisTemplate.opsForValue().set(TokenConstants.ROLE_ID+userKey,roleId,20,TimeUnit.MINUTES);
        return token;
    }
}
